package org.finesys.common.websocket.handler;

import org.finesys.common.websocket.support.WebSocketHolder;
import org.springframework.lang.NonNull;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.messaging.support.ChannelInterceptor;

import lombok.extern.slf4j.Slf4j;

/**
 * 用于传输向webSocket客户端发送的消息
 */
@Slf4j
public class WebSocketClientOutboundChannel implements ChannelInterceptor {
    @Override
    public void postSend(@NonNull Message<?> message,@NonNull MessageChannel channel, boolean sent) {
        StompHeaderAccessor accessor = StompHeaderAccessor.wrap(message);
        if (accessor.getCommand() == null) {
            return;
        }
        //获取用户信息
        String userName = WebSocketHolder.getUserName(accessor);
        //这里的sessionId和accountId对应HttpSessionIdHandshakeInterceptor拦截器的存放key
        switch (accessor.getCommand()) {
            case CONNECT:
                log.info("============>当前登录用户{}, 建立websocket连接", userName);
                break;
            case CONNECTED://链接成功
                break;
            case DISCONNECT:
                log.info("============>当前登录用户{}, 断开websocket连接", userName);
                break;
            case ERROR:
                log.error("============>当前登录用户{},websocket连接错误", userName);
                break;
            default:
        }
    }
}
